//index.js
//获取应用实例
const app = getApp()
var util = require('../../utils/util.js');
import { HTTP } from '../../utils/https.js'
let http = new HTTP()
Page({
  data: {
    stylegreen:'background:#37c777; color:8c8c8c;',
    stylegray:'background:#eaeaea; color:8c8c8c;',
    city: '', //城市
    region_id:'', //城市id
    list:[], 
    listxuqiu:['全部','招人','租赁'],
    career_id:'', //职业id
    currentindex:1, //标题选中项
    currenlistindex:0,
    currenlistindexrencai:0,
    sexcurrent: '', //筛选条件性别选中
    heightcur: '', //筛选身高
    pricecur: '', //筛选价格
    showleftnav:false, //左侧导航控制器
    //性别
      sexItems: [ 
        { name: '男生', value: '1'},
        { name: '女生', value: '2' },
      ],
    //身高
      heightItems: [],
    //价格
      pricetItems: [],
    //limit  页数展示
      limit:10, 
    //page
      page:1,
    //人才数据
      talentlist:[],
    //人才无数据
      nodata: true, //人才无数据
      nodatademand:true,
    //需求的无数据
      nodataneed: true,
    //需求类型
      typeneed:'',
    //需求列表
      demandlist:[],
    //需求page 
     pageneed:1,
     word:'', //需求搜索关键词
     nodatedemand:true,
     nodate:true,
     hascurcolor:'no', //人才筛选增加选择颜色
     hasshowname:true, //是否登录
  },
  //获取当前城市
    city:function(){
        var that = this;
        http.request({
          url: 'mini/v1/index/position',
          success: function (res) {
            wx.hideLoading();
            that.setData({
              city: res.data.name,
              region_id: res.data.region_id
            })

            that.talent(1, that.data.limit)
          }
        })
    },
  //获取选择条件
    indexAll:function(){
      var that = this;
      const data = {
        group_by: 'indexAll'
      }
      http.request({
        url: 'mini/v1/index/talent',
        data:data,
        success: function (res) {
          wx.hideLoading();
          that.setData({
            heightItems: res.data.height,
            pricetItems: res.data.signing,
            list:res.data.career
          })
        }
      })
    },
  //获取首页列表人才
    talent:function(page,limit,more){
      var that = this;
      const data = {
        sex: this.data.sexcurrent,
        region_id: this.data.region_id,
        career_id: this.data.career_id,
        height: this.data.heightcur,
        signing: this.data.pricecur,
        page:page,
        word: this.data.word,
        limit: limit
      }

      http.request({
        url: 'mini/v1/index/talent',
        data: data,
        success: function (res) {
          wx.hideLoading();
          wx.stopPullDownRefresh()
          if (!!more){
            that.setData({
              talentlist: that.data.talentlist.concat(res.data.data),
              nodate: true
            })

            if (res.data.data.length == 0) {
              that.setData({
                nodata: false
              });
            }
          }else{
            that.setData({
              talentlist: res.data.data,
              nodate: true
            })

            if (res.data.data.length == 0) {
              that.setData({
                nodate: false
              });
            }
          }
          
        }
      })
    },
  //获取首页列表 需求
    demand: function (page, limit, more) {
      var that = this;
      const data = {
        type: this.data.typeneed,
        page: page,
        limit: limit,
        word: this.data.word
      }

      http.request({
        url: 'mini/v1/index/demand',
        data: data,
        success: function (res) {
          wx.hideLoading();
          wx.stopPullDownRefresh()
          var data = res.data.data;
          for (var i = 0; i < data.length; i++) {
            data[i].created_at = util.timestampToTime(data[i].created_at);
          } 

          if (!!more) {
            that.setData({
              demandlist: that.data.demandlist.concat(res.data.data),
              nodatedemand:true
            })

            if (res.data.data.length == 0) {
              that.setData({
                nodatademand: false
              });
            }
          }else {
            that.setData({
              demandlist: res.data.data,
              nodatedemand:true
            })

            if (res.data.data.length == 0) {
              that.setData({
                nodatedemand: false
              });
            }
          }
        }
      })
    },
  onLoad: function (options) {
    this.indexAll();
    this.city();
    this.demand(1, 10);
  },
  onShareAppMessage: function () {
    return util.share()
  },
  onShow:function(){
    if(wx.getStorageSync('checklogin') == 'checklogin'){
      this.setData({
         hasshowname:true
      })
   }

    if (!!app.globalData.checkup) { //判是从首页哪个标题点击的  这里是发布完需求点击进来的
      this.setData({
        currentindex: 2
      })
      
      var type = app.globalData.checkup == 'what' ? 'lease' : 'job';
      if(type == 'job'){
        this.setData({
          currenlistindex:1
        })
      }else{
        this.setData({
          currenlistindex: 2
        })
      }
      
      this.setData({
        typeneed:type
      })
      this.demand(1, 10);
      
    } else { //判是从首页哪个标题点击的  这里是首页人才
      if (wx.getStorageSync('dataindex') == 'dataindex') { //判是从首页哪个标题点击的  这里是登陆页点击进来的
        this.indexAll();
        this.city();
        this.demand(1, 10);
        wx.removeStorageSync('dataindex');
      }
    //设置城市展示
      else { //判是从首页哪个标题点击的  这里是切换完城市列表进来的
        if(!!wx.getStorageSync('checkcity')) {
          this.setData({
            city: wx.getStorageSync('checkcity').fullname,
            region_id: wx.getStorageSync('checkcity').id,
            nodata: true,
            page:1
          })

          this.talent(1, 10);
          wx.removeStorageSync('checkcity');
          this.setData({
            currentindex: 1
          })
        }
      }
    }
  },
onPullDownRefresh() {
  this.setData({
    page: 1,
    pageneed: 1
  })
  if (this.data.currentindex == 1){
    this.talent(1, this.data.limit)
  }else{
    this.demand(1, this.data.limit);
  }
},
onReachBottom: function () {
  if (this.data.currentindex == 1) {
    this.setData({
      page: this.data.page + 1
    })

    if (this.data.talentlist.length> 0){
      this.talent(this.data.page, this.data.limit, 'more');
    }
  } else {
    this.setData({
      pageneed: this.data.pageneed + 1
    })
    this.demand(this.data.pageneed, this.data.limit, 'more');
  }
},
//获取顶部选中事件
    getcurtitle(e){
      this.setData({
        currentindex: e.currentTarget.dataset.index,
        page:1,
        pageneed:1,
        word: ''
      })

      if(this.data.currentindex == 1){
        app.globalData.checkup = null;
        if (this.data.talentlist.length <= 0){
          this.setData({
            nodate: false
          })
        }
        this.talent(1, 10); 
      }

      if (this.data.currentindex == 2) {
        this.setData({
          nodate:true
        })
        this.demand(1, 10);
      }
  }, 
//获取职业列表选中事件
    bindlist:function(e){
      app.globalData.checkup = null;
      this.setData({
        currenlistindex: e.currentTarget.dataset.index,
        pageneed: 1
      })
      
      if(this.data.currenlistindex == 0){
        this.setData({
          typeneed: ''
        })
      }
      else if (this.data.currenlistindex == 1){
        this.setData({
          typeneed:'job'
        })
      }else{
        this.setData({
          typeneed: 'lease'
        })
      }
      this.demand(1, this.data.limit);
    },
 //选择性别
  sexChange(e){
    var sexcurrent;
    if (e.currentTarget.dataset.index == this.data.sexcurrent) {
      sexcurrent = '';
    } else {
      sexcurrent = e.currentTarget.dataset.index
    }

    this.setData({
      sexcurrent: sexcurrent
    })
  },
//选择身高
  heightChange(e){
    var heightcur;
    if (e.currentTarget.dataset.index == this.data.heightcur){
      heightcur = '';
    }else{
      heightcur = e.currentTarget.dataset.index
    }

    this.setData({
      heightcur: heightcur
    })
  },
//选择价格
  pricetChange(e){
    var pricecur;
    if (e.currentTarget.dataset.index == this.data.pricecur) {
      pricecur = '';
    } else {
      pricecur = e.currentTarget.dataset.index
    }

    this.setData({
      pricecur: pricecur
    })
  },
//全部清空
  delete:function(){
    this.setData({
      pricecur: '',
      heightcur: '',
      sexcurrent: '',
    })
  },
//左侧导航出现
  ctroleft:function(){
    this.setData({
      showleftnav:true
    })
  },
//确定筛选演员
  surechoose:function(){
    this.setData({
      showleftnav:false
    })

    if (!!this.data.pricecur || !!this.data.heightcur || !!this.data.sexcurrent ){
      this.setData({
        hascurcolor:'hascurcolor'
      })
    }else{
      this.setData({
        hascurcolor: 'no'
      })
    }
   
    this.talent(1, this.data.limit)
  },
//人才导航筛选
  bindlistnav:function(e){
    this.setData({
      currenlistindexrencai: e.currentTarget.dataset.index,
      career_id: e.currentTarget.dataset.index,
      nodata:true,
      page:1
    })
    
    this.talent(1, this.data.limit)
  },
//关闭层
  closeceng:function(){
    this.setData({
      showleftnav: false
    })
  },
  gosearch:function(e){
    this.setData({
      word: e.detail.value
    })

    if(this.data.currentindex == 1){
      this.talent(1, 10);
    }else{
      this.demand(1, 10);
    }
    
  },
    //判断是否登录
    checklogins(){
      if(wx.getStorageSync('checklogin')!== 'checklogin'){
        wx.showModal({
          title: '提示',
          content: '登录后才可查看操作，是否进行登录?',
          confirmColor: '#f45859',
          confirmText: '去登录',
          success(res) {
            if (res.confirm) {
              wx.redirectTo({
                url: '../../other/login/login',
                success: res => { 
                },
              },
              );
            } else if (res.cancel) {
              wx.navigateBack({ changed: true });
            }
          }
        })
      };
    }
})
